package com.wuhan.template.module.home.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wuhan.template.R;
import com.wuhan.template.base.BaseApplication;
import com.wuhan.template.base.BaseViewHolder;
import com.wuhan.template.base.MyBaseAdapter;
import com.wuhan.template.module.home.activity.DianBoItemActivity;
import com.wuhan.template.module.home.entity.DianBoEntity;

import java.text.SimpleDateFormat;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2016/5/16 0016.
 */
public class DianBoBaseAdapter extends MyBaseAdapter<DianBoEntity>{
    private Context context;
    private ImageLoader loader;
    private DisplayImageOptions options;
    private String url;

    public DianBoBaseAdapter(Context context, List<DianBoEntity> list) {
        super(context, list);
        this.context = context;
        loader  = ((BaseApplication)(context.getApplicationContext())).getImageLoader();
        options = new DisplayImageOptions.Builder()
                .cacheOnDisk(true)
                .cacheInMemory(true)
                .build();
    }

    @Override
    public int getLayoutId() {
        return R.layout.dianbo_list_item;
    }

    @Override
    public BaseViewHolder getViewHolder() {

        return new ViewHolder();
    }

    @Override
    public void bindViews(View view, BaseViewHolder viewHolder) {

        ViewHolder holder = (ViewHolder) viewHolder;
        final DianBoEntity item = getItem(holder.position);
        holder.title.setText(item.getName());
        SimpleDateFormat format = new SimpleDateFormat("MM月dd日");
        String time = format.format(Long.parseLong(item.getContent_update_time()));
        holder.time.setText("最后更新时间:   "+time);
        url = item.getIndexpic().getHost()+ item.getIndexpic().getDir()+ item.getIndexpic().getFilepath()+ item.getIndexpic().getFilename();
        loader.displayImage(url,holder.image,options);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,DianBoItemActivity.class);
                intent.putExtra("column_id", item.getChilds());
                intent.putExtra("imageUrl", item.getIndexpic().getHost()+ item.getIndexpic().getDir()+
                        item.getIndexpic().getFilepath()+ item.getIndexpic().getFilename());
                intent.putExtra("text",item.getContent());
                intent.putExtra("title",item.getName());

                context.startActivity(intent);
            }
        });
    }

    public static class ViewHolder extends BaseViewHolder{

        @BindView(R.id.dianbo_title)
        TextView title;
        @BindView(R.id.dianbo_time)
        TextView time;
        @BindView(R.id.dianbo_image)
        ImageView image;
    }
}
